package com.prdiot.poetry.controller;

import com.prdiot.poetry.domains.Poems;
import com.prdiot.poetry.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.TemplateEngine;

import java.util.List;

@Controller
public class PoemListController {

    @Autowired
    PoemsService poemsService;

    @Autowired
    TagsService tagsService;

    @Autowired
    ChengyuService chengyuService;

    @Autowired
    MingjuService mingjuService;

    @Autowired
    AuthorService authorService;

    @GetMapping("/poems")
    public String poems(Model model,
                        @RequestParam(value="filter",required=false,defaultValue="") String filter,
                        @RequestParam(value="type",required=false,defaultValue="none") String type,
                        @RequestParam(value="page",required=false,defaultValue="1") String page){

        int curPage = 1;
        try{
            curPage = Math.abs(Integer.valueOf(page));
        }catch (Exception exception){
            curPage = 1;
        }
        String pageTitle = "";
        int pageCount = 0;
        Sort sort = new Sort(Sort.Direction.ASC, "code");
        Pageable pageable = PageRequest.of(curPage - 1, 10, sort);
        Page<Poems> poemsPage = null;
        String filterText = "";
        if (type.equals("tag")){
            poemsPage = poemsService.searchPoemsByTag(filter,pageable);
            filterText = filter;
            pageTitle = "关于" + filterText + "的诗词列表-古诗词文网";
        }else if (type.equals("author")){
            poemsPage = poemsService.searchPoemsByAuthorId(filter,pageable);
            filterText = authorService.getAuthorNameByAuthorId(filter);
            pageTitle = "关于诗人" + filterText + "的诗词列表-古诗词文网";
        }else if (type.equals("dynasty")){
            poemsPage = poemsService.searchPoemsByDynastyId(filter,pageable);
            filterText = poemsService.getDynastyNameById(filter);
            pageTitle = filterText + "的诗词列表-古诗词文网";
        }else if (type.equals("type")){
            poemsPage = poemsService.searchPoemsByType(filter,pageable);
            filterText = filter;
            pageTitle = filterText + "形式的诗词列表-古诗词文网";
        }
        List<Poems> list = null;
        if (poemsPage == null){
            list = poemsService.getRandPoetry();
            pageCount = 999999;
            pageTitle = "诗词列表大全-古诗词文网";
        }else{
            list = poemsService.attachTag(poemsPage.getContent());
            pageCount = poemsPage.getTotalPages();
        }



        model.addAttribute("pageTitle", pageTitle);
        model.addAttribute("poemsList", list);
        model.addAttribute("page", curPage);
        model.addAttribute("pageCount", pageCount);
        model.addAttribute("filterText", filterText);
        model.addAttribute("filter", filter);
        model.addAttribute("filterType", type);
        model.addAttribute("tagList",tagsService.getTagsOrderByPoemCount(100));
        model.addAttribute("mingjuList",mingjuService.getRandMingju());
        model.addAttribute("themeList",mingjuService.getMingjuTheme());
        model.addAttribute("chengyuList",chengyuService.getRandPoetry());
        model.addAttribute("tagFilter",tagsService.getTagsOrderByPoemCount(50));
        model.addAttribute("authorsFilter",authorService.getTopAuthors(50));
        model.addAttribute("dynastyFilter",poemsService.getDynastyList());
        return "poems";
    }


}
